$(document).ready(function() {

    var content_width = 0;
    var page_height = 0;

    var stretch_contentpanel = function() {
        var total_width = $(window).width();
        var db_side = $("#db_selection").width();
        var table_side = $("#table_selection").width();
        var target = $("#form_view");

        var target_new_witdh = total_width - (db_side + table_side) * 1;
        target.width(target_new_witdh);

        // Get content width
        content_width = $("#form_view .box").width();
    };

    var stretch_sidepanels = function() {
        var total_height = $(document).height();
        var top_height = $("#top").height();
        var new_height = (total_height-top_height) * 1;

        var db_side = $("#db_selection").height(new_height);
        var table_side = $("#table_selection").height(new_height);

        page_height = new_height;
    };

    var set_interface_dimensions = function(width,height) {
        $.get('datagrid/js/'+width+'/'+height);
    };

    var submit_fs_form = function() {
        var form = $("#fs_form")
        var form_values = $("#fs_form").serialize();
        var destination = $("#fs_form").attr("action");
        var class = "warning";

        $.post(destination, form_values,
        function(data) {
            var msg = jQuery.parseJSON(data);
            if(msg.success == 1) {
                class = "success";
            } else {
                class = "error";
            }

            // Show message
            var popup = $('<div class="alert-message '+class+'"><p>'+msg.msg+'</p></div>').hide();

            $(".actions").after(popup);
            popup.fadeIn(600).delay(2000).fadeOut(600);
        });

        return false;
    };



    

    /* Event launches */
    $(window).resize(function() {
        stretch_contentpanel();
        stretch_sidepanels();
    });

    /* Init all nesseary funcitons */
    var init = function() {
        stretch_contentpanel();
        stretch_sidepanels();

        set_interface_dimensions(content_width, page_height);

        $("#btnSubmitFS").click(submit_fs_form);
    };

    init();
});